﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Evaluant.Uss.ObjectContext;
using Evaluant.Uss;

namespace SDBTest
{
    [TestFixture]
    public class StupidTests
    {
        ObjectService os = null;

        [SetUp]
        public void Init()
        {
            os = new ObjectService();

            using (var oc = os.CreateObjectContext())
                oc.InitializeRepository();
        }

        //[Test]
        public void TestLagreOgHente300()
        {
            using (var oc = os.CreateObjectContext())
            {
                oc.BeginTransaction();
                for (int i = 0; i < 300; i++)
                {
                    JallaSub js = new JallaSub();
                    js.SubName = "JS " + i.ToString();
                    oc.Serialize(js);
                }

                oc.CommitTransaction();
            }

            using (var oc = os.CreateObjectContext())
            {
                var alt = oc.Load<JallaSub>();

                Assert.AreEqual(300, alt.Count);
            }
        }

        //[Test]
        public void TestCompoundUpdate()
        {
            Jalla j = new Jalla();
            j.Name = "Ny";
            string id = j.Id;

            JallaSub js = new JallaSub();
            js.SubName = "Suben";
            string subid = js.Id;

            using (var oc = os.CreateObjectContext())
            {
                oc.BeginTransaction();
                oc.Serialize(j);
                oc.Serialize(js);
                oc.CommitTransaction();
            }

            using (var oc = os.CreateObjectContext())
            {
                Jalla j2 = oc.LoadWithId<Jalla>(id);

                oc.BeginTransaction();
                j2.Dato = DateTime.Now;
                j2.Tall = 15;
                j2.Name = "Ny igjen";

                JallaSub sub2 = oc.LoadWithId<JallaSub>(subid);
                Assert.IsNotNull(sub2);

                j2.Sub = sub2;

                oc.Serialize(j2);

                oc.CommitTransaction();
            }
        }

        [Test]
        public void Test()
        {
            string jid;
            DateTime dt = new DateTime(2009, 10, 10, 13, 24, 50);
            using (ObjectContext oc = os.CreateObjectContext())
            {
                Jalla j = new Jalla();
                jid = j.Id;
                j.Name = "Heisann";
                j.Tall = 87564;
                j.Dato = dt;

                j.Sub = new JallaSub() { SubName = "Sub sitt navn" };

                j.Sub2 = new JallaSub() { SubName = "Sub 2" };

                oc.BeginTransaction();
                oc.Serialize(j);
                oc.CommitTransaction();
            }

            using (ObjectContext oc = os.CreateObjectContext())
            {
                Jalla j2 = oc.LoadWithId<Jalla>(jid);
                Assert.IsNotNull(j2);

                Assert.AreEqual(87564, j2.Tall);
                Assert.AreEqual(dt, j2.Dato);

                j2.Name = "Lagret tilbake";

                var t = j2.Sub;
                Assert.IsNotNull(t);

                oc.BeginTransaction();
                oc.Serialize(j2);
                oc.CommitTransaction();
            }

            using (ObjectContext oc = os.CreateObjectContext())
            {
                Jalla j2 = oc.LoadWithId<Jalla>(jid);
                Assert.AreEqual("Lagret tilbake", j2.Name);
            }

            using (ObjectContext oc = os.CreateObjectContext())
            {
                Jalla j3 = oc.Load<Jalla>("[Name ^ 'Lag' or Name = 'Lag']").FirstOrDefault();
                Assert.IsNotNull(j3);
            }

            //Test reference infer
            using (ObjectContext oc = os.CreateObjectContext())
            {
                Query q = new Query(typeof(Jalla));
                q.InferredRelationships.Add("Sub");
                q.InferredRelationships.Add("Sub2");

                Jalla result = oc.Load<Jalla>(q)[0];

                Assert.IsNotNull(result.Sub);
                Assert.IsNotNull(result.Sub2);
            }
        }
    }


    public class Jalla
    {
        protected string m_Id = Guid.NewGuid().ToString();
        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        protected string m_Name = string.Empty;
        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        protected int m_Tall = 0;
        public int Tall
        {
            get { return m_Tall; }
            set { m_Tall = value; }
        }

        protected DateTime m_Dato = DateTime.Now;
        public DateTime Dato
        {
            get { return m_Dato; }
            set { m_Dato = value; }
        }

        protected JallaSub m_Sub = null;
        public virtual JallaSub Sub
        {
            get { return m_Sub; }
            set { m_Sub = value; }
        }

        protected JallaSub m_Sub2 = null;
        public virtual JallaSub Sub2
        {
            get { return m_Sub2; }
            set { m_Sub2 = value; }
        }
    }

    public class JallaSub
    {
        protected string m_Id = Guid.NewGuid().ToString();
        public string Id
        {
            get { return m_Id; }
            set { m_Id = value; }
        }

        protected string m_SubName = string.Empty;
        public string SubName
        {
            get { return m_SubName; }
            set { m_SubName = value; }
        }
    }
}
